package cn.dnui_xcz826.dnuifood_16110100826.controller;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.dnui_xcz826.dnuifood_16110100826.R;
import cn.dnui_xcz826.dnuifood_16110100826.adapter.FoodListAdapter;
import cn.dnui_xcz826.dnuifood_16110100826.bean.CollectBean;
import cn.dnui_xcz826.dnuifood_16110100826.bean.FoodBean;
import cn.dnui_xcz826.dnuifood_16110100826.bean.ResultBean;
import cn.dnui_xcz826.dnuifood_16110100826.listener.onRetrofitListener;
import cn.dnui_xcz826.dnuifood_16110100826.model.CollectModel;
import cn.dnui_xcz826.dnuifood_16110100826.model.ShopModel;


public class FoodListActivity extends BaseActivity implements onRetrofitListener, View.OnClickListener {
    private Context context;
    private String shop_id="",shop_name="",result="";
    private TextView shopname;
    private ImageView go_back,collect;
    private RecyclerView recyclerView;
    private FoodListAdapter adapter;

    void initViews(){
        setLayout(R.layout.activity_food_list);
        context=FoodListActivity.this;
        //初始化界面控件 View
        recyclerView = findViewById(R.id.rw_fl);
        shopname=findViewById(R.id.shop_id);
        go_back=findViewById(R.id.back);
        collect=findViewById(R.id.foodcollect);

        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);
    }
    void initEvents(){
        go_back.setOnClickListener(this);
        collect.setOnClickListener(this);
        shop_name=getIntent().getStringExtra("shop_name");
        shopname.setText(shop_name);
        //处理【返回】和【收藏】按钮的点击事件
        //【返回】按钮调用 finish()关闭当前界面
        //【收藏】按钮调用 collect()方法
    }
    void initDatas(){
        //通过 Intent 获取店铺名称
        shop_id=getIntent().getStringExtra("shop_id");
        //获取店铺的菜谱列表，调用 getFoodList ()方法
        getFoodList();
        //获取店铺的收藏状态，调用 iscollected ()方法
        iscollected();
    }
    //发送获取店铺的菜谱列表的网络请求
    private void getFoodList() {
        ShopModel foodModel=new ShopModel();
        foodModel.getFoodByShop(shop_id,this);
    }
    //发送获取店铺的收藏状态的网络请求
    private void iscollected(){
        CollectModel foodModel=new CollectModel();
        foodModel.isCollected(getUser_id(), shop_id,"0",this);
    }
    //发送对店铺的收藏信息进行修改的网络请求
    private void collect(){
        CollectModel collectModel=new CollectModel();
        collectModel.userCollectShop(getUser_id(),shop_id,this);
    }
    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag)
        {
            case Constants.GETFOODBYSHOP:
            //获取店铺的菜谱列表 getFoodByShop
                String phonenum=getIntent().getStringExtra("phonenum");
                adapter = new FoodListAdapter(FoodListActivity.this, (List<FoodBean>)o, R.layout.food_item,phonenum);
                recyclerView.setAdapter(adapter);
            break;
            case Constants.ISCOLLECTED:
            //获取店铺的收藏状态 isCollected
                result=((CollectBean)o).getCollected().toString();
                if (result.equals("0")) {
                    collect.setImageResource(R.drawable.ic_uncollection);
                }
                else {
                    collect.setImageResource(R.drawable.ic_collection);
                }
            break;
            case Constants.USERCOLLECTSHOP:
            //对店铺的收藏信息进行修改 userCollectShop
                if (((ResultBean)o).getSuccess().equals("1")) {
                    if (result.equals("0")) {
                        collect.setImageResource(R.drawable.ic_collection);
                        Toast.makeText(FoodListActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                        result = "1";
                    } else {
                        collect.setImageResource(R.drawable.ic_uncollection);
                        Toast.makeText(FoodListActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                        result = "0";
                    }
                } else {
                    Toast.makeText(FoodListActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

//    @Override
//    public void onSuccess(Object object, int flag) {
//        adapter = new FoodListAdapter(FoodListActivity.this, (List<FoodBean>)object, R.layout.food_item);
//                recyclerView.setAdapter(adapter);
//    }


    @Override
    public void onFailure(String msg) {
        Toast.makeText(FoodListActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.foodcollect:
                collect();
                break;
        }
    }

}
